package com.hitqz.robot.biz.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hitqz.robot.api.business.dto.RobotCommonPathDto;
import com.hitqz.robot.api.business.entity.RobotLockPathEntity;
import com.hitqz.robot.biz.mapper.RobotLockPathMapper;
import com.hitqz.robot.biz.service.RobotLockPathService;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 机器人管理 服务实现类
 * </p>
 *
 * @author xiaojiezhang
 * @since 2021-12-20
 */
@Service
public class RobotLockPathServiceImpl extends ServiceImpl<RobotLockPathMapper, RobotLockPathEntity> implements RobotLockPathService {

    private final String lock_A=new String("robotLockPath");

    /**
     * @param robotSn
     * @return
     */
    @Override
    public List<RobotLockPathEntity> getByRobotSn(String robotSn) {
        return baseMapper.getByRobotSn(robotSn);
    }

    @Override
    public RobotLockPathEntity getByLockName(String lockName) {
        return baseMapper.getByLockName(lockName);
    }

    @Override
    public int deleteByCommonPath(String commonPath) {
        return baseMapper.deleteByCommonPath(commonPath);
    }

    @Override
    public int deleteByRobotSn(String robotSn) {
        return baseMapper.deleteByRobotSn(robotSn);
    }

    @Override
    public int deleteByRobotSnAndCommpathName(String robotSn, String commonPath) {
        return baseMapper.deleteByRobotSnAndCommpathName(robotSn,commonPath);
    }

    @Override
    public int deleteNotExist(String commonPathNameF, String commonPathNameS, String commonPathNameT) {
        return baseMapper.deleteNotExist(commonPathNameF, commonPathNameS, commonPathNameT);
    }
    /**
     * 批量保存
     *
     * @param commonPathDto
     * @return
     */
    @Override
    public Integer insert(String robotSn, RobotCommonPathDto commonPathDto, Boolean deleteFlag) {
        synchronized (lock_A) {
            try {
                if(deleteFlag){
                    baseMapper.deleteOthers(robotSn,commonPathDto.getCommonPathName());
                }
                RobotLockPathEntity dbLockPath = baseMapper.getByLockName(commonPathDto.getCommonPathName());
                if(dbLockPath!=null && dbLockPath.getRobotSn().equalsIgnoreCase(robotSn)){
                    return 1;
                }
                RobotLockPathEntity lockPath = new RobotLockPathEntity();
                lockPath.setRobotSn(robotSn);
                lockPath.setLockPath(commonPathDto.getCommonPathName());
                lockPath.setStartPos(commonPathDto.getStartPos().getCode());
                lockPath.setEndPos(commonPathDto.getEndPos().getCode());
                lockPath.setPathPos(commonPathDto.getCommonPathPos());
                lockPath.setDirection("null");
                if(commonPathDto.getRobotSnA().equalsIgnoreCase(robotSn)) {
                    lockPath.setNotifyRobot(commonPathDto.getRobotSnB());
                }else{
                    lockPath.setNotifyRobot(commonPathDto.getRobotSnA());
                }
                return baseMapper.insert(lockPath);
            }catch (Exception e){
                return 0;
            }
        }
    }
}
